package com.gg.bananvideo;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.gg.bananvideo.adapter.RectclerAdapter;
import com.gg.bananvideo.databinding.FragmentFirstBinding;
import com.gg.bananvideo.model.ListResponseData;
import com.gg.bananvideo.ui.VideoActivity;
import com.gg.bananvideo.util.JsonUtil;
import com.gg.bananvideo.util.constant;
import okhttp3.*;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.List;

public class FirstFragment extends Fragment {

    private FragmentFirstBinding binding;
    public static ListResponseData.Data.Vodrows datas;

    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {

        binding = FragmentFirstBinding.inflate(inflater, container, false);

        Log.d("FirstFragment", "onCreateView:");

        return binding.getRoot();

    }

    Handler handler = new Handler(msg -> {
        List<ListResponseData.Data.Vodrows> data = (List<ListResponseData.Data.Vodrows>) msg.obj;
        RectclerAdapter adapter = new RectclerAdapter(data);
        adapter.setClickListener((data1, position) -> {
            Intent intent = new Intent(getActivity(), VideoActivity.class);
            datas = data1;
            startActivity(intent);
        });
        binding.root.setLayoutManager(new LinearLayoutManager(this.getContext()));
        binding.root.setAdapter(adapter);
        return false;
    });

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Log.d("FirstFragment", "onViewCreated:");


        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(constant.BASE_URL)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                try {
                    assert response.body() != null;
                    String dataString = response.body().string();
                    ListResponseData resp = JsonUtil.toBean(dataString, ListResponseData.class);
                    List<ListResponseData.Data.Vodrows> data = resp.getData().getVodrows();
                    Log.i("response", dataString);
                    Message msg = new Message();
                    msg.obj = data;
                    handler.sendMessage(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                e.printStackTrace();
            }
        });
        //binding.buttonFirst.setOnClickListener(v ->
        //        NavHostFragment.findNavController(FirstFragment.this)
        //                .navigate(R.id.action_FirstFragment_to_SecondFragment)
        //);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.d("FirstFragment", "onDestroyView");
        binding = null;
    }


}
